﻿#include "OiPCH.hpp"
#include <QtCharts/QChart>
#include <QtCharts/QValueAxis>

namespace Oi
{
    using namespace PLScannerCore;
    static QFont getFont(int size)
    {
        QFont font;
        font.setPixelSize(size);
        return font;
    }
    namespace PLScannerDll
    {
        class FramePageImpl : public Impl<FramePage>
        {
        public:
            void init();
            void updateParams();
            void createChart();
        public:
            QVBoxLayout* _mainLayout = nullptr;

        public:
            ProductParams _params;

        public:
            ToolProfilesView* _profile;
            LineView* _tread;
            QChartView* _halfChart ;
        public:
            double _edgeThickness;
            int    _precision;

        };

        void FramePageImpl::init()
        {
            OI_Q(FramePage);
            
            QVBoxLayout* mainLayout = new QVBoxLayout(q);

            _profile = new ToolProfilesView();

            _profile->setPageFlag(PMP_ThicknessMeasure);

            _tread = new LineView();
            _tread->setTol(0.08, 0.05);
            _profile->setParams(_params);
            _halfChart = new QChartView(q);

            QGridLayout* botLayout = new QGridLayout();

            mainLayout->addWidget(_profile, 3);
            mainLayout->addWidget(_tread, 1);
            mainLayout->addWidget(_halfChart,2);
            _profile->winId();

            QMetaObject::invokeMethod(q, "onReady");
        }

        void FramePageImpl::updateParams()
        {
            OI_Q(FramePage);

            _profile->setParams(_params);
            
            OiProfile pfs;

            pfs.push_back(QPointF( -_params.materialsWidth / 2, 0));
            pfs.push_back(QPointF(-_params.materialsWidth / 4, +0.1));
            pfs.push_back(QPointF(+_params.materialsWidth / 4, -0.1));
            pfs.push_back(QPointF(+_params.materialsWidth / 2, 0));

            _tread->display(pfs);
            _tread->zoomFit();
        }

        FramePage::FramePage(QWidget * parent /*= nullptr*/)
            : PageAbstract(parent)
        {
            OI_I(FramePage)->init();
        }

        void FramePage::setParams(const ProductParams& params)
        {
            OI_F(FramePage);
            d->_params = params;
            d->updateParams();
        }

        int FramePage::setResult(const PLScannerCore::WorkResult::Result& result)
        {
            OI_F(FramePage);
            PLScannerCore::WorkResult::Result resultTemp = result;
            d->_params.getMeasureEdgeThickness(d->_edgeThickness);
            d->_params.getMeasurePrecision(d->_precision);
            d->_profile->setResult(result);

            double v1 = 0.0, v2 = 0.0, v3 = 0.0, v4 = 0.0, v5 = 0.0, v6 = 0.0, v7 = 0.0 ,vdis = 0.0;
            
            QChart* chart = new QChart();
            chart->setTitle(QStringLiteral("HalfSeries"));

            d->_halfChart->setChart(chart);//将chart添加到chartview中
            d->_halfChart->chart()->legend()->setFont(QFont("Arial", 19));
            //创建折线序列
            QLineSeries* series0 = new QLineSeries();
            QLineSeries* series1 = new QLineSeries();

            series0->setName(QStringLiteral("op"));
            series1->setName(QStringLiteral("ds"));

            chart->addSeries(series0);//将序列添加到图表
            chart->addSeries(series1);

            //序列添加数值
            auto rProfiles = result.profile;
            if (rProfiles.size() > 50)
            {
                double sum = 0;
                for (int i = 0; i < rProfiles.size(); i++)
                {
                    if (rProfiles[i].x()<= 0)
                    {
                        series0->append(qAbs(rProfiles[i].x()), rProfiles[i].y());
                    }
                    else
                    {
                        series1->append(qAbs(rProfiles[i].x()), rProfiles[i].y());
                    }
                   
                }
               
            }
            //创建坐标轴
            QValueAxis* axisX = new QValueAxis;
            axisX->setRange(0, d->_params.materialsWidth/2+50);
            axisX->setTitleText("time(secs)");

            QValueAxis* axisY = new QValueAxis;
            axisY->setRange(0, 4);
            axisY->setTitleText("value");

            chart->setAxisX(axisX, series0);//为序列添加坐标轴
            chart->setAxisY(axisY, series0);
            chart->setAxisX(axisX, series1);
            chart->setAxisY(axisY, series1);
            return 0;
        }

        void FramePage::onReady()
        {
            OI_F(FramePage);
            d->_profile->zoomFit();
            d->_tread->zoomFit();
        }

        void FramePage::resizeEvent(QResizeEvent *event)
        {
            OI_F(FramePage);
            d->_profile->zoomFit();
            d->_tread->zoomFit();
        }

        void FramePage::setRun(bool flag)
        {
            OI_F(FramePage);
        }

        void FramePage::toCurrent()
        {
            OI_F(FramePage);
        }


    }
}